<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="因子名称" prop="status">
        <el-input v-model="queryParams.targetname" placeholder="请输入因子名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain 
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-arrow-left"
          size="mini"
          @click="goback"
        >返回</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="pageTablelist" style="width: 100%"  v-if="refreshTable" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column  type="selection" width="55" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  type="index" label="序号"  width="55" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="bussinesscategorycode" label="业务类别编码" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="bussinesscategory" label="业务类别" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="itemname" label="因子名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="hourshowpointbits" label="小时标准位数" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="absolutepointbits" label="修约位数" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="standardcategory" label="水质标准名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="isevalute" label="因子是否参与评价" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="sequence" label="监测项目排序" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>

          </template>
        </el-table-column>
    </el-table>
     <el-pagination layout="total,sizes,prev, pager, next" :total="this.total" background :page-size="10"  :page-sizes="[10, 20, 50]"  @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form"label-width="200px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类别编码" prop="bussinesscategorycode">
              <el-input :disabled="true" v-model="form.bussinesscategorycode" placeholder="请输入业务类别编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类别" prop="bussinesscategory">
              <el-input v-model="form.bussinesscategory" placeholder="请输入业务类别" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="因子名称" prop="cnname">
              <el-select v-model="form.itemname" placeholder="请选择因子名称" style="width: 100%" @change="formChange($event,'id',{'itemname':'cnname'},wateritemList)">
                <el-option
                  v-for="dict in wateritemList"
                  :key="dict.id"
                  :label="dict.cnname"
                  :value="dict.cnname"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小时标准位数" prop="hourshowpointbits">
              <el-input v-model="form.hourshowpointbits" placeholder="请输入小时标准位数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="修约位数" prop="absolutepointbits">
              <el-input v-model="form.absolutepointbits" placeholder="请输入小时标准位数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="水质标准名称" prop="standardcategory">
              <el-select v-model="form.standardcategory" placeholder="请选择水质标准名称" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.zk_water_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="因子是否参与评价" prop="isevalute">
              <el-radio v-model="form.isevalute" label="1">是</el-radio>
              <el-radio v-model="form.isevalute" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="是否展示" prop="remark3">
              <el-radio v-model="form.remark3" label="1">是</el-radio>
              <el-radio v-model="form.remark3" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监测项目排序" prop="sequence">
              <el-input v-model="form.sequence" placeholder="请输入监测项目排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="均值计算方式" prop="avgcalculateway">
              <el-input v-model="form.avgcalculateway" placeholder="请输入均值计算方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修约方式" prop="datedealway">
              <el-input v-model="form.datedealway" placeholder="请输入均值计算方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="自动站常规五项+高猛酸盐+总磷+氨氮 为1" prop="remark1">
              <el-radio v-model="form.remark1" label="1">是</el-radio>
              <el-radio v-model="form.remark1" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标记自动站监测因子是否只有整四点数据" prop="remark2">
              <el-radio v-model="form.remark2" label="1">是</el-radio>
              <el-radio v-model="form.remark2" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否参与汛期污染强度计算因子" prop="remark4">
              <el-radio v-model="form.remark4" label="1">是</el-radio>
              <el-radio v-model="form.remark4" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getBussinessitemsList ,bussinessitemsAdd,bussinessitemsEdit,bussinessitemsRemove,getWateritemDistinctitemlist} from '@/api/dataResources/monitorData/dmTos.js';
export default {
    dicts:[
    "water_quality_level",
    "rh_monitor_factor"
  ],
  props:{
    businesscategorycode:{
      type:String,
      default:''
    }
  },
  computed:{
    tableList(){
      let list = this.bussinessitemsList.filter(item => item.bussinesscategorycode == this.businesscategorycode);
      if(this.queryParams.targetname){
        this.page = 1;
        list = list.filter(item => item.itemname.indexOf(this.queryParams.targetname) > -1);
      }
      return list;
    },
    pageTablelist(){
      return this.tableList.slice((this.page - 1) * this.limit, this.page * this.limit);
    },
    total(){
      return this.tableList.length;
    }
  },
  data(){
    return {
      showSearch: true,
      page: 1,
      limit: 10,
      queryParams:{
        targetname:'',
      },
      bussinessitemsList:[],
      wateritemList:[],
      tableSelect:[],
      refreshTable:true,
      loading: false,
      title:'',
      open:false,
      form: {},
    }
  },
  created() {
    this.getWateritemDistinctitemlistFn();
    this.getList();
  },

  methods: {
    formChange(value,key,map,list){
      let item = list.find(it => it[key]==value);
      if(item) {
        for(let fkey in map){
          this.form[fkey] = item[map[fkey]]
          console.log('fkey',fkey,this.form[fkey])
        }
      }
    },
    goback(){
      this.$emit('goback')
    },
    async getWateritemDistinctitemlistFn(){
      this.wateritemList = [];
      let res = await getWateritemDistinctitemlist();
      if(res.code ==200) {
        this.wateritemList = res.rows;
      }
    },
    handleQuery() {
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.title = "添加";
      this.form = {
        "bussinesscategorycode": this.businesscategorycode,
        "bussinesscategory": "",
        "itemname": "",
        "itemcode": "",
        "sequence": null,
        "avgcalculateway": null,
        "datedealway": null,
        "keeppointbits": "",
        "hourshowpointbits": null,
        "absolutepointbits": null,
        "relativepointbits": null,
        "maxvalue": null,
        "minvalue": null,
        "startdate": null,
        "enddate": null,
        "remark1": null,
        "remark2": null,
        "remark3": "",
        "remark4": null,
        "remark5": null,
        "remark6": null,
        "isevalute": "",
        "order": "",
        "standardcategory": null,
        "waterbody": null
      }; 
    },
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑";
      this.form = {...row};

    },
    async handleDelete(row = null) {
      let id;
      if(row){
        id = row.id;
      } else if(this.tableSelect.length > 0){
        id = this.tableSelect.map(item => item.id).join(',');
      };
      await bussinessitemsRemove(id);
      this.getList();
    },
    handleSelectionChange(val){
      this.tableSelect = val;
    },
    handleExport() {

    },
    resetQuery() {
      this.queryParams = {};
      this.getList();
    },
    handleSizeChange(val){
      this.page = 1;
      this.limit = val;
    },
    handleCurrentChange(val) { 
      this.page = val;
    },
    
    submitForm(){
      this.$refs["form"].validate((valid) =>{
        if(valid){
          this.saveByApi();
        }
      })
    }, 
    async saveByApi(){
      this.open = false;
      if(this.form.id){
        await bussinessitemsEdit(this.form);
      } else {
        await bussinessitemsAdd(this.form);
      }
      await this.getList();
    },
    cancel() {
      this.open = false;
    },
    async getList() {
      console.log(this.queryParams);
      this.tableSelect = [];
      this.loading = true
      let res = await getBussinessitemsList({ bussinesscategorycode: this.businesscategorycode });
      if(res.code =='200') {
        this.bussinessitemsList = res.rows;
      }
      this.loading = false;
    },
  },
};
</script>

<style></style>
